package com.bos.service.impl;

import com.bos.common.PageData;
import com.bos.common.ResultData;
import com.bos.data.model.BosProjectModel;
import com.bos.data.model.BosUserModel;
import com.bos.data.repositories.UserRepository;
import com.bos.data.repositories.jpa.UserJPARepository;
import com.bos.mapper.UserMapper;
import com.bos.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("userServiceImpl")
public class UserServiceImpl implements UserService {
    private Logger logger=LoggerFactory.getLogger(UserServiceImpl.class);
    @Autowired
    UserMapper userMapper;

    @Autowired
    private UserJPARepository userJPARepository;

    @Autowired
    private UserRepository userRepository;
    //用户注册
    @Transactional
    @Override
    public boolean register(BosUserModel user) {
        boolean isSuc = false;
        if (user != null) {
            int row = userMapper.register(user);
            if (row > 0) {
                isSuc = true;
            }
        }
        return isSuc;
    }

    //用户登录
    @Override
    public BosUserModel login(BosUserModel user) {
        BosUserModel u = null;
        if (user.getName() != null && user.getPassword() != null) {
            u = userMapper.login(user);
        }
        return u;
    }

    //将用户信息保存到数据库中
    @Override
    public int insertUser(BosUserModel user) {
        return userMapper.insertUser(user);
    }

    @Override
    public List<BosUserModel> findUser() {
        return userMapper.findUser();
    }

    @Override
    public BosUserModel findUserByName(String name) {
        return userMapper.findUserByName(name);
    }

    //根据id查询出bos_Project中的数据
    @Override
    public List<BosProjectModel> findProjectByName(String name) {
        return userMapper.findProjectByName(name);
    }

    @Override
    public List<BosUserModel> findUsers() {
        return userMapper.findUsers();
    }


    /**
     * 修改用户登录密码
     * @param uid
     * @param newPass
     * @return
     */
    @Transactional
    @Override
    public int updatePass(int uid,String newPass){
        try {
            return userJPARepository.updatePass(uid,newPass);
        }catch (Exception e){
            logger.error("修改用户登录密码错误");
            return 0;
        }

    }

    @Override
    public ResultData getRole(int uid){
        try {
            ResultData result=new ResultData();
            List list= userRepository.getRole(uid);
            result.setPageData(new PageData(list));
            result.setResult("true");
            result.setMessage("查询成功");
            return result;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }
}
